<?php
/**
 * Base class for admincp controlelr
 *
 * @package     nuo
 * @subpackage  nuo.controller.admincp
 * @author      Nam Nguyen <nam.ngvan@gmail.com>
 * @version     1.0
 * @since       Apr 19, 2013
 */
class Nuo_Controller_Admincp_Setting extends Nuo_Controller_Admincp
{
	/**
	 * Form name
	 * @access  public
	 * @var     string
	 */
	protected $_form = '';
	
	protected $_breadcrum = array('title'=>'Settings','toolbar'=>'admincp/core');

	/**
	 *
	 * @access  public
	 * @param   Nuo_Request  $request
	 * @return  string/array
	 */
	function execute($request)
	{
		$form = Nuo::getObject($this -> _form);
		
		//Nuo::setVar('breadcrum', $this -> _breadcrum);
		
		//$form->setTitle('');
		
		$config = Nuo::getApi('core/config');

		$children = $form -> getChildren();

		if ($request -> isGet())
		{

			foreach ($children as $ele)
			{
				$name = $ele -> getName();
				list($module, $group, $key) = explode('_', $name, 3);
				$group = $module . '/' . $group;
				$ele -> setValue(Nuo::getConfig($group, $key));
			}
		}

		if ($request -> isPost() && $form -> isValid($_POST))
		{
			$data = $form -> getData();

			Nuo::getApi('core/config') -> write($data);

			Nuo_Messenger::instance()->add(Intl::__('Updated!'));

			Nuo_Cache::instance()-> remove('core/config');
		}

		

		return Nuo::getView() -> render('core/view/controller/admincp/edit', $data = array('form' => $form));
	}

}
